package hu.blok1.veilingsite.client.gui;

import hu.blok1.veilingsite.client.Veilingsite;
import hu.blok1.veilingsite.client.data.AccountService;
import hu.blok1.veilingsite.client.data.AccountServiceAsync;
import hu.blok1.veilingsite.shared.Address;
import hu.blok1.veilingsite.shared.User;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class createAccountGUI {
	private VerticalPanel vp;
	private Label errorLabel = new Label();
	private TextBox tb_name = new TextBox();
	private TextBox tb_lastname = new TextBox();
	private TextBox tb_email = new TextBox();
	private PasswordTextBox tb_pass1 = new PasswordTextBox();
	private PasswordTextBox tb_pass2 = new PasswordTextBox();
	private TextBox tb_bankaccount = new TextBox();
	private TextBox tb_street = new TextBox();
	private TextBox tb_housenumber = new TextBox();
	private TextBox tb_postcode = new TextBox();
	private TextBox tb_place = new TextBox();
	private TextBox tb_country = new TextBox();
	private TextBox tb_phonenumber = new TextBox();
	private Button b_create;
	private Button b_clear;

	private Grid grid;
	private Veilingsite veiling;
	private User user;
	private User newUser;

	private final AccountServiceAsync accountService = GWT
			.create(AccountService.class);

	public createAccountGUI(VerticalPanel a, Veilingsite v) {
		veiling = v;
		user = veiling.getUser();
		if (!user.isLoggedIn()) {
			init(a);
		} else {
			init_loggedin(a);
		}
	}

	public void init(VerticalPanel a) {
		vp = a;
		vp.clear();
		grid = new Grid(13, 2);
		grid.setText(0, 0, "Voornaam:");
		grid.setWidget(0, 1, tb_name);
		grid.setText(1, 0, "Achternaam:");
		grid.setWidget(1, 1, tb_lastname);
		grid.setText(2, 0, "E-mailadres:");
		grid.setWidget(2, 1, tb_email);
		grid.setText(3, 0, "Wachtwoord:");
		grid.setWidget(3, 1, tb_pass1);
		grid.setText(4, 0, "Wachtwoord (nogmaals):");
		grid.setWidget(4, 1, tb_pass2);
		grid.setText(5, 0, "Straatnaam:");
		grid.setWidget(5, 1, tb_street);
		grid.setText(6, 0, "Huisnummer:");
		grid.setWidget(6, 1, tb_housenumber);
		grid.setText(7, 0, "Postcode:");
		grid.setWidget(7, 1, tb_postcode);
		grid.setText(8, 0, "Plaats:");
		grid.setWidget(8, 1, tb_place);
		grid.setText(9, 0, "Land:");
		grid.setWidget(9, 1, tb_country);
		grid.setText(10, 0, "Telefoon-nummer:");
		grid.setWidget(10, 1, tb_phonenumber);
		grid.setText(11, 0, "Bankrekening:");
		grid.setWidget(11, 1, tb_bankaccount);
		grid.setWidget(12, 0, b_create = new Button("Aanmaken"));
		grid.setWidget(12, 1, b_clear = new Button("Clear"));
		vp.add(grid);
		vp.add(errorLabel);
		b_create.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				create();
			}
		});

		b_clear.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				clear();
			}
		});
	}

	public void init_loggedin(VerticalPanel a) {
		vp = a;
		vp.clear();
		vp.add(new Label(
				"U bent al ingelogd, en hoort deze pagina helemaal niet te zien!"));
	}

	public void create() {
		StringBuilder errorMessage = new StringBuilder();
		if (tb_name.getText().equals(""))
			errorMessage.append("\nOntbrekende Voornaam");
		
		if (!tb_name.getText().matches("^[A-Za-z-\\s]{3,30}$"))
			errorMessage.append("\nOnjuiste Voornaam ingevuld");

		if (tb_lastname.getText().equals(""))
			errorMessage.append("\nOntbrekende Achternaam");
		
		if (!tb_lastname.getText().matches("^[A-Za-z-\\s]{3,50}$"))
			errorMessage.append("\nOnjuiste Achternaam ingevuld");

		if (tb_email.getText().equals(""))
			errorMessage.append("\nE-mailadres is niet ingevuld");

		if (!tb_email
				.getText()
				.matches(
						"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"))
			errorMessage.append("\nGeen geldig e-mailadres ingevuld");

		if (!tb_pass1.getText().matches("^.{8,20}$"))
			errorMessage.append("\nWachtwoord niet lang genoeg of te lang.");

		if (!tb_pass1.getText().equals(tb_pass2.getText()))
			errorMessage.append("\nWachtwoorden komen niet overeen");

		if (tb_street.getText().equals(""))
			errorMessage.append("\nStraatnaam is niet ingevuld");
		
		if (!tb_street.getText().matches("^[A-Za-z-\\s]{2,100}$"))
			errorMessage.append("\nOngeldige straatnaam ingevuld");

		if (tb_housenumber.getText().equals(""))
			errorMessage.append("\nHuisnummer is niet ingevuld");

		if (!tb_housenumber.getText().matches("^[0-9]{1,5}[a-zA-z]?$"))
			errorMessage.append("\nOngeldig huisnummer");
		
		if(tb_postcode.getText().equals(""))
			errorMessage.append("\nPostcode niet ingevuld");
		
		if(!tb_postcode.getText().matches("^[0-9]{4}[a-zA-z]{2}$"))
			errorMessage.append("\nOngeldige postcode");
		
		if (tb_place.getText().equals(""))
			errorMessage.append("\nPlaats is niet ingevuld");

		if (!tb_place.getText().matches("^.{1,200}$"))
			errorMessage.append("\nOnjuiste plaatsnaam ingevuld");
		
		if (tb_country.getText().equals(""))
			errorMessage.append("\nLand is niet ingevuld");
		
		if (!tb_country.getText().matches("^[A-Za-z-\\s]{2,100}$"))
			errorMessage.append("\nOnjuist land ingevuld");

		if (tb_phonenumber.getText().equals(""))
			errorMessage.append("\nTelefoonnummer is niet ingevuld");

		if (!tb_phonenumber.getText().matches("^[0-9]{8,20}$"))
			errorMessage.append("\nTelefoonnummer is niet geldig");

		if (tb_bankaccount.getText().equals(""))
			errorMessage.append("\nBankrekeningnummber is niet ingevuld");
		
		if (!tb_bankaccount.getText().matches("^[0-9]{7,10}$"))
			errorMessage.append("\nBankrekening onjuist ingevuld");

		if (!errorMessage.toString().isEmpty()) {
			Window.alert(errorMessage.toString());
		} else {
			newUser = new User(0, tb_name.getText(),
					tb_lastname.getText(), tb_email.getText(),
					tb_pass1.getText(), new Date(), "User",
					tb_bankaccount.getText(), 0, "Active", 0);
			Address newAddress = new Address(0, tb_street.getText(),
					tb_housenumber.getText(), tb_postcode.getText(), 
					tb_place.getText(), tb_country.getText(),
					tb_phonenumber.getText());
			accountService.createAccount(newUser, newAddress,
					new AsyncCallback<java.lang.Boolean>() {
						public void onFailure(Throwable caught) {
							System.out
									.println("something went wrong, no connection with the server probs...");
						}

						public void onSuccess(Boolean result) {
							if (result) {
								Window.alert("Welkom " + newUser.getName());
								login(newUser);
							} else {
								Window.alert("Er is al een gebruiker met dit email adres.");
							}
						}
					});
		}
	}

	public void clear() {
		tb_name.setText("");
		tb_lastname.setText("");
		tb_email.setText("");
		tb_pass1.setText("");
		tb_pass2.setText("");
		tb_street.setText("");
		tb_housenumber.setText("");
		tb_postcode.setText("");
		tb_place.setText("");
		tb_country.setText("");
		tb_phonenumber.setText("");
		tb_bankaccount.setText("");
	}
	
	public void login(User loginUser){
		accountService.logIn(loginUser, new AsyncCallback<User>(){
			public void onFailure(Throwable caught) {
				Window.alert("Helaas hebben wij u niet direct kunnen inloggen.");
				History.newItem("login");
			}

			@Override
			public void onSuccess(User result) {
				if(result.isLoggedIn()){
					veiling.setUser(result);
					veiling.reloadHeader();
					History.newItem("home");
				}else{
					Window.alert("Helaas hebben wij u niet direct kunnen inloggen.");
					History.newItem("login");
				}
			}
		});
	}
}